﻿using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace LY.Common.Utils
{
    public class UploadImages : Controller
    {
        static string urlPath = string.Empty;
        static string[] _url;
        public UploadImages(string path, string[] url)
        {
            var applicationPath = VirtualPathUtility.ToAbsolute("~") == "/" ? "" : VirtualPathUtility.ToAbsolute("~");
            urlPath = applicationPath + "/" + path;
            _url = url;
        }

        public ActionResult UpLoadProcess(UploadParameters para, ref ReturnParameters path)
        {
            string filePathName = string.Empty;
            string savePath = _url[para.op ?? 0].ToString();

            string localPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, savePath);
             //string localPath = + path;
            //string domain = @"C:\zc\";
            //string localPath = Path.Combine(domain, savePath);

            //if (Request.Files.Count == 0)???
            //{
            //    return Json(new { jsonrpc = 2.0, error = new { code = 102, message = "保存失败" }, id = "id" });
            //}

            string ex = Path.GetExtension(para.file.FileName);


            filePathName = DateTime.Now.ToString("yyyyMMdd") + Guid.NewGuid().ToString("N") + ex;
            if (!System.IO.Directory.Exists(localPath))
            {
                System.IO.Directory.CreateDirectory(localPath);
            }

            var fullnewfilePath = Path.Combine(localPath, filePathName);

            para.file.SaveAs(fullnewfilePath);


            var newfilePath = "../" + Path.Combine(savePath, filePathName);
            //添加水印  
            LY.Common.WaterMark.AddImageSignPic(newfilePath, newfilePath, "watermark.png", 2, 100, 5);

            //

            path.id = para.id;
            path.filePath = "/" + savePath + "/" + filePathName;
            path.type = para.op;

            return Json(new
            {
                jsonrpc = "2.0",
                id = para.id,
                filePath = urlPath + "/" + filePathName,
                op = para.op
            });

        }

        public string GetImageUrlToDB(int? op, string path)
        {
            return "/" + _url[op ?? 0] + "/" + path.Split('/')[2]; ;
        }
    }

    public class UploadParameters
    {
        public string id { get; set; }
        public string name { get; set; }
        public string type { get; set; }
        public string lastModifiedDate { get; set; }
        public int size { get; set; }
        public HttpPostedFileBase file { get; set; }
        public int? op { get; set; }
    }

    public class ReturnParameters
    {
        public string id { get; set; }
        public int? type { get; set; }
        public string filePath { get; set; }
    }
}
